package com.atguigu.gmall.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.app.BaseAppV1;
import com.atguigu.gmall.realtime.common.Constant;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/8/27 11:31
 */
public class DwmUserJumpDetail extends BaseAppV1 {
    public static void main(String[] args) {
        new DwmUserJumpDetail().init(3002, 1, "DwmUserJumpDetail", "DwmUserJumpDetail", Constant.TOPIC_DWD_PAGE);
        
    }
    
    @Override
    protected void run(StreamExecutionEnvironment env,
                       DataStreamSource<String> sourceStream) {
        sourceStream = env.fromElements(
            "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000} ",
            "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":11000} ",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000}",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                "\"home\"},\"ts\":29999} ",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                "\"detail\"},\"ts\":50000} "
        );
        
        KeyedStream<JSONObject, String> stream = sourceStream
            .map(JSON::parseObject)
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner(((page, ts) -> page.getLong("ts")))
            )
            .keyBy(page -> page.getJSONObject("common").getString("mid"));
        
        // 1. 定义模式
        Pattern<JSONObject, JSONObject> pattern = Pattern
            .<JSONObject>begin("entry")
            .where(new SimpleCondition<JSONObject>() {
                @Override
                public boolean filter(JSONObject value) throws Exception {
                    String lastPageId = value.getJSONObject("page").getString("last_page_id");
                    
                    return lastPageId == null || lastPageId.length() == 0;
                    
                }
            })
            .next("secondPage")
            .where(new SimpleCondition<JSONObject>() {
                @Override
                public boolean filter(JSONObject value) throws Exception {
                    JSONObject page = value.getJSONObject("page");
                    String pageId = page.getString("page_id");
                    String lastPageId = page.getString("last_page_id");
                    return pageId != null && lastPageId != null && lastPageId.length() > 0;
                }
            })
            .within(Time.seconds(10));
        // 2. 把模式应用到流上
        PatternStream<JSONObject> ps = CEP.pattern(stream, pattern);
        // 3. 取出匹配或者超时的数据
        SingleOutputStreamOperator<JSONObject> normal = ps
            .select(
                new OutputTag<JSONObject>("timeout") {},
                new PatternTimeoutFunction<JSONObject, JSONObject>() {
                    @Override
                    public JSONObject timeout(Map<String, List<JSONObject>> pattern,
                                              long timeoutTimestamp) throws Exception {
                        // 超时数据才是我们需要
                        List<JSONObject> list = pattern.get("entry");
                        return list.get(0);
                    }
                },
                new PatternSelectFunction<JSONObject, JSONObject>() {
                    @Override
                    public JSONObject select(Map<String, List<JSONObject>> pattern) throws Exception {
                        // 正常匹配模式的, 对我们来说不需要
                        return null;
                    }
                }
            );
    
        normal.getSideOutput(new OutputTag<JSONObject>("timeout") {}).print();
    }
}
/*

数据源: 页面日志


    这条日志是入口:  last_page_id 这个字段是空
    后面没有跟着其他日志:
    
    


 */
